/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.global;

import java.io.File;
import java.util.ArrayList;

import ontocompo.ui.appmanagement.Application;
import ontocompo.ui.semantic.SemanticInfo;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.adl.Factory;
import org.objectweb.fractal.adl.FactoryFactory;

import fr.inria.acacia.corese.api.EngineFactory;
import fr.inria.acacia.corese.api.IEngine;
import fr.inria.acacia.corese.exceptions.EngineException;

public class Resources {
//	@SuppressWarnings("serial")
//	public static ArrayList<Application> ONTOCOMPOAPPLICATIONS = new ArrayList<Application>(){{
//		add(new Application("Trim String Application", "applications.trimstring.ui.TrimStringApp", new SemanticInfo("trimstring", "http://rainbow.i3s.unice.fr/applications/trimstring")));
//		add(new Application("Reverse String Application", "applications.reversestring.ui.ReverseStringApp", new SemanticInfo("reversestring", "http://rainbow.i3s.unice.fr/applications/reversestring")));
//		add(new Application("Insurance Card Application", "applications.insurancecard.ui.InsuranceCardApp", new SemanticInfo("InsuranceCard", "http://rainbow.i3s.unice.fr/applications/insurancecard")));
//		add(new Application("Business Directory Application", "applications.businessdirectory.ui.BusinessDirectoryApp", new SemanticInfo("BusinessDirectory", "http://rainbow.i3s.unice.fr/applications/businessdirectory")));
//	}};
	
	public static ArrayList<Application> ONTOCOMPOAPPLICATIONS = new ArrayList<Application>();
	
	public static ArrayList<Application> LOADEDAPPLICATIONS = new ArrayList<Application>();
	
	public static String ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH = "./OntoApplications/";
	
	public static void buildAvailableApplications() {
		SAXBuilder sxb = new SAXBuilder();
		
		File directoryToScan = new File(ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + "list/");
		File[] files = directoryToScan.listFiles();
		
		for(File f : files) {
			if(f.getName().endsWith(".ontoapp")) {
				Document document = null;
				try
				{
					document = sxb.build(f);
				}
				catch(Exception e){
					e.printStackTrace();
				}
		
				Element racine = document.getRootElement();
				
				String name = racine.getChildText("name");
				String uicomponent = racine.getChildText("uicomponent");
				String appdir = racine.getChildText("appdir");
				String ontographname = racine.getChildText("ontographname");
				
				ONTOCOMPOAPPLICATIONS.add(new Application(name, uicomponent, new SemanticInfo(appdir, ontographname)));
			}
		}
	}
	
	private static Factory FRACTALFACTORY = null;
	
	public static Factory getFractalFactory() {
		if(Resources.FRACTALFACTORY == null) {
			try {
				Resources.FRACTALFACTORY = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
			} catch (ADLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Resources.FRACTALFACTORY;
	}
	
	//public static String ROOT_DIRECTORY_PATH = "E:/Christian/workspace/OntoCompoSemantic/"; //PC Philippe
	// public static String ROOT_DIRECTORY_PATH = "/Users/christianbrel/Documents/Home/Etudes/These_et_Monitorat/Travaux/ontoappcompo/OntoCompoSemantic/"; //MacBookPro
	//public static String ROOT_DIRECTORY_PATH = "/Users/christianbrel/Documents/Home/These/Travaux/ontoappcompo/OntoCompoSemantic/"; //iMac
	//public static String ROOT_DIRECTORY_PATH = "../OntoCompoSemantic/";
	public static String ROOT_DIRECTORY_PATH = "";
	
	public static String SCHEMAS_DIRECTORY_NAME = "schemas/";
	public static String RULES_DIRECTORY_NAME = "rules/";
	//public static String APPLICATIONS_DIRECTORY_NAME = "applications/";
	
	private static IEngine CORESEENGINE = null;
	
	
	
	public static IEngine getCoreseEngine() {
		if(Resources.CORESEENGINE == null) {
			try {
				EngineFactory ef = new EngineFactory();
				Resources.CORESEENGINE = ef.newInstance();
				Resources.CORESEENGINE.load(Resources.ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + Resources.SCHEMAS_DIRECTORY_NAME);
				Resources.CORESEENGINE.load(Resources.ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + Resources.RULES_DIRECTORY_NAME);
				
				// run rules
				Resources.CORESEENGINE.runRuleEngine();
			} catch(EngineException e) {
				e.printStackTrace();
			}
		}
		return Resources.CORESEENGINE;
	}
	
	public static void addResourceToCoreseEngine(String resource) {
		IEngine coreseEngine = Resources.getCoreseEngine();
		
		try {
			coreseEngine.load(resource);
			
			// run rules
			coreseEngine.runRuleEngine();
		} catch(EngineException e) {
			e.printStackTrace();
		}
	}
}
